﻿using System;
using System.Globalization;
using System.Web.UI.WebControls;
using ContractManagementSystem.BusinessLogic;
using ContractManagementSystem.Entity;

namespace ContractManagementSystem
{
    public partial class ViewContractorByRole : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                MessageLabel.Text = "";
                ListContractorPanel.Visible = true;
                EditContractorPanel.Visible = false;
                SearchContractor();
            }
        }

        public void SearchContractor()
        {
            if (!string.IsNullOrEmpty(FullNameTextBoxSearch.Text))
            {
                ViewAllContractListView.DataSource = ContractorBusiness.SearchByRole(FullNameTextBoxSearch.Text,Session["Name"].ToString());
                ViewAllContractListView.DataBind();
            }
            else if (Session["Name"]!=null)
            {
                ViewAllContractListView.DataSource = ContractorBusiness.FetchByRole(UserBusiness.FetchRoleByUserName(Session["Name"].ToString()), Session["Name"].ToString());
                ViewAllContractListView.DataBind();
            }
            else
            {
                Response.Redirect("LogOn.aspx");
            }
        }

        protected void FullNameTextBoxTextChanged(object sender, EventArgs e)
        {
            SearchContractor();
        }

        protected void DataPager1_OnPreRender(object sender, EventArgs e)
        {
            //ViewContractor();
            SearchContractor();
        }

        protected void FilterLinkButton_OnClick(object sender, EventArgs e)
        {
            SearchContractor();
        }

        protected void Submit_Click(object sender, EventArgs e)
        {
            var contractorEntity = new ContractorEntity
            {
                ContractorId = new Guid(ContractorIdTexBox.Text),
                FullName = FullNameTextBox.Text,
                Birthday = DateTime.Parse(BirthDateTextBox.Text, CultureInfo.CurrentCulture),
                Gender = GenderDropDownList.SelectedIndex,
                Email = EmailTextBox.Text,
                Address = AddressTextBox.Text,
                PhoneNumber1 = PhoneNumberTextBox.Text,
                PhoneNumber2 = PhoneNumbersTextBox.Text,
                Branch = BranchTextBox.Text,
                IsActive = true
            };
            MessageLabel.Text = ContractorBusiness.Update(contractorEntity) ? "Update Success !" : "Insert False !";
            ListContractorPanel.Visible = true;
            EditContractorPanel.Visible = false;
            MessageLabel.Text = "";
        }
        protected void Back_OnClick(object sender, EventArgs e)
        {
            MessageLabel.Text = "";
            ListContractorPanel.Visible = true;
            EditContractorPanel.Visible = false;
            SearchContractor();
        }

        protected void ViewAllContractListView_OnItemDeleting(object sender, EventArgs e)
        {

        }

        protected void ViewAllContractListView_OnItemEditing(object sender, EventArgs e)
        {

        }

        protected void EditLinkButton_OnCommand(object sender, CommandEventArgs e)
        {
            ListContractorPanel.Visible = false;
            EditContractorPanel.Visible = true;

            var contractorId = ViewAllContractListView.Items[Convert.ToInt32(e.CommandArgument.ToString(),CultureInfo.CurrentCulture)].FindControl("ContractorId") as Label;
            if (contractorId != null) ContractorIdTexBox.Text = contractorId.Text;

            var fullName = ViewAllContractListView.Items[Convert.ToInt32(e.CommandArgument.ToString(), CultureInfo.CurrentCulture)].FindControl("FullName") as Label;
            if (fullName != null) { FullNameTextBox.Text = fullName.Text; }

            var birthDate = ViewAllContractListView.Items[Convert.ToInt32(e.CommandArgument.ToString(), CultureInfo.CurrentCulture)].FindControl("BirthDate") as Label;
            if (birthDate != null) { BirthDateTextBox.Text = birthDate.Text; }

            var gender = ViewAllContractListView.Items[Convert.ToInt32(e.CommandArgument.ToString(), CultureInfo.CurrentCulture)].FindControl("Gender") as Label;
            if (gender != null) GenderDropDownList.SelectedValue = gender.Text;

            var email = ViewAllContractListView.Items[Convert.ToInt32(e.CommandArgument.ToString(), CultureInfo.CurrentCulture)].FindControl("Email") as Label;
            if (email != null) EmailTextBox.Text = email.Text;

            var address = ViewAllContractListView.Items[Convert.ToInt32(e.CommandArgument.ToString(), CultureInfo.CurrentCulture)].FindControl("Address") as Label;
            if (address != null) AddressTextBox.Text = address.Text;

            var phoneNumber1 = ViewAllContractListView.Items[Convert.ToInt32(e.CommandArgument.ToString(), CultureInfo.CurrentCulture)].FindControl("PhoneNumber1") as Label;
            if (phoneNumber1 != null) PhoneNumberTextBox.Text = phoneNumber1.Text;

            var phoneNumber2 = ViewAllContractListView.Items[Convert.ToInt32(e.CommandArgument.ToString(), CultureInfo.CurrentCulture)].FindControl("PhoneNumber2") as Label;
            if (phoneNumber2 != null) PhoneNumbersTextBox.Text = phoneNumber2.Text;

            var branch = ViewAllContractListView.Items[Convert.ToInt32(e.CommandArgument.ToString(), CultureInfo.CurrentCulture)].FindControl("Branch") as Label;
            if (branch != null) BranchTextBox.Text = branch.Text;
        }

        protected void DeleteButton_OnCommand(object sender, CommandEventArgs e)
        {
            var contractorEntity = new ContractorEntity { ContractorId = new Guid(e.CommandArgument.ToString()) };
            ContractorBusiness.Delete(contractorEntity);
            SearchContractor();
        }

        protected void ActiveDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            SearchContractor();
        }
    }
}
